package org.graduation.recruitment.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Nullable;
import org.graduation.recruitment.entity.dto.EnterpriseRegisterDTO;
import org.graduation.recruitment.entity.dto.UserLoginDTO;
import org.graduation.recruitment.entity.dto.UserRegisterDTO;
import org.graduation.recruitment.entity.exception.ServiceException;
import org.graduation.recruitment.entity.pojo.EnterpriseUser;
import org.graduation.recruitment.entity.pojo.User;
import org.graduation.recruitment.mapper.EnterpriseUserMapper;
import org.graduation.recruitment.service.EnterpriseUserService;
import org.graduation.recruitment.utils.Encrypt;
import org.graduation.recruitment.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EnterpriseUserServiceImpl extends ServiceImpl<EnterpriseUserMapper, EnterpriseUser> implements EnterpriseUserService {
    @Autowired
    private Encrypt encrypt;
    @Autowired
    private EnterpriseUserMapper enterpriseUserMapper;
    @Autowired
    private Token token;

    @Nullable
    @Override
    public String login(UserLoginDTO user) {
        user.setPassword(encrypt.md5_32(user.getPassword()));
        EnterpriseUser enterpriseUser = enterpriseUserMapper.login(user);
        if (enterpriseUser != null && enterpriseUser.getPassword().equals(user.getPassword())) {
            enterpriseUser.hidePassword();
            return token.generateToken(enterpriseUser);
        } else {
            return null;
        }
    }

    @Override
    public boolean register(UserRegisterDTO user) {
        EnterpriseRegisterDTO dto = (EnterpriseRegisterDTO) user;
        dto.setPassword(encrypt.md5_32(dto.getPassword()));
        return enterpriseUserMapper.insert(dto.toEnterpriseUser()) > 0;
    }

    @Override
    public User getUserInfoByToken(String tokenStr) {
        Long enterpriseUid = token.getUidByToken(tokenStr);
        return getUserInfoById(enterpriseUid);
    }

    @Override
    public User getUserInfoById(Long id) {
        return enterpriseUserMapper.selectById(id);
    }

    @Override
    public boolean updatePassword(Long id, String oldPassword, String newPassword) {
        EnterpriseUser enterpriseUser = enterpriseUserMapper.selectOne(idAndPasswordQueryWrapper(id, encrypt.md5_32(oldPassword)));
        if (enterpriseUser == null) {
            throw new ServiceException("原密码错误");
        } else {
            return enterpriseUserMapper.update(updatePasswordWrapper(id, encrypt.md5_32(newPassword))) == 1;
        }
    }

    @Override
    public boolean updateInfo(User user) {
        return enterpriseUserMapper.update(updateInfoWrapper((EnterpriseUser) user)) == 1;
    }

    private LambdaQueryWrapper<EnterpriseUser> idAndPasswordQueryWrapper(Long id, String password) {
        return new LambdaQueryWrapper<EnterpriseUser>().eq(EnterpriseUser::getId, id).eq(EnterpriseUser::getPassword, password);
    }

    private LambdaUpdateWrapper<EnterpriseUser> updatePasswordWrapper(Long id, String password) {
        return new LambdaUpdateWrapper<EnterpriseUser>()
                .eq(EnterpriseUser::getId, id)
                .set(EnterpriseUser::getPassword, password);
    }

    private LambdaUpdateWrapper<EnterpriseUser> updateInfoWrapper(EnterpriseUser user) {
        return new LambdaUpdateWrapper<EnterpriseUser>()
                .eq(EnterpriseUser::getId, user.getId())
                .set(EnterpriseUser::getPhone, user.getPhone())
                .set(EnterpriseUser::getEmail, user.getEmail())
                .set(EnterpriseUser::getContactPerson, user.getContactPerson())
                .set(EnterpriseUser::getDescription, user.getDescription());
    }
}
